//
//  XMImgView.m
//  XMPlayer
//
//  Created by min on 2017/10/31.
//  Copyright © 2017年 min. All rights reserved.
//

#import "XMImgView.h"
#import "XMPlayerModel.h"
#import "XMPlayer.h"
@interface XMImgView ()

/** 图片按钮 */
@property (nonatomic,strong) UIButton *imgPlayBtn;

@end

@implementation XMImgView

-(CGFloat)initWithStrHeigh :(NSString * )str
{
    if (ScreenHeight == 667 || ScreenHeight == 736) {
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth-20, ScreenHeight*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_18 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+15;
    }else{
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth-20, ScreenHeight*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_16 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+15;
    }
}
- (void)setPlayerModel:(XMPlayerModel *)playerModel{
    
    _playerModel = playerModel;

    CGFloat height = [self initWithStrHeigh:playerModel.title];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth-20, height)];
    self.titleLabel.font = FONT_16;
    self.titleLabel.textColor = RGBColor(51, 51, 51);
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.text = [NSString stringWithFormat:@"%@",playerModel.title];
    [self addSubview: self.titleLabel];
 
    
    _imgPlayBtn = [[UIButton alloc] init];
    NSURL *url = [NSURL URLWithString:playerModel.pic_url];
//    NSData *data = [NSData dataWithContentsOfURL:url];
    // 这里最好用SDWebImage框架加载图片
//    [_imgPlayBtn setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
    [_imgPlayBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"大的默认图片"]];
     _imgPlayBtn.width = playerModel.img_w;
    _imgPlayBtn.height = playerModel.img_h;
//    _imgPlayBtn.x = (WIDTH - _imgPlayBtn.width)/2.0;
//    _imgPlayBtn.y = ([playerModel cellHeight] - _imgPlayBtn.height)/2.0;
    _imgPlayBtn.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame)+5, ScreenWidth, playerModel.img_h);
    [_imgPlayBtn addTarget:self action:@selector(btnPlayClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_imgPlayBtn];

    UIImageView *playImgView = [[UIImageView alloc] init];
    playImgView.image = [UIImage imageNamed:@"视频播放"];
    playImgView.width = playImgView.height = 44;
    playImgView.x = (_imgPlayBtn.width - playImgView.width)/2.0;
    playImgView.y = (_imgPlayBtn.height - playImgView.height)/2.0;
    [_imgPlayBtn addSubview:playImgView];
 
    self.pvLabel = [[UILabel alloc]initWithFrame:CGRectMake(10,CGRectGetMaxY(_imgPlayBtn.frame)+10, ScreenWidth/2-10, 18)];
    self.pvLabel.font = FONT_13;
    self.pvLabel.textColor = RGBColor(51, 51, 51);
    self.pvLabel.text =  [NSString stringWithFormat:@"来源: %@",playerModel.source];
    [self addSubview: self.pvLabel];
    
    self.uptimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.pvLabel.frame),CGRectGetMaxY(_imgPlayBtn.frame)+10, ScreenWidth/2-20, 18)];
    self.uptimeLabel.font = FONT_13;
    self.uptimeLabel.textColor = RGBColor(51, 51, 51);
    self.uptimeLabel.textAlignment = NSTextAlignmentRight;
     self.uptimeLabel.text =   [LSZSpecialPackaging GetThePreviousTimeAndCompareItNow:playerModel.uptime];
    [self addSubview: self.uptimeLabel];
}

// 点击播放
- (void)btnPlayClick:(UIButton *)sender{

    NSLog(@"点击播放");
    XMPlayerManager *playerManager = [[XMPlayerManager alloc] init];
    playerManager.sourceImagesContainerView = self;
    playerManager.currentImage = sender.currentImage;
    playerManager.videourl = self.playerModel.video_url;
    playerManager.titleStr = self.playerModel.title;
    playerManager.shareURL = self.playerModel.pic_url;
    [playerManager show];

}


@end
